﻿using Microsoft.AspNetCore.Mvc;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Dto;
using WisDom.SmartTMS.Entities;
using WisDom.SmartTMS.Enums;
using WisDom.SmartTMS.Interface;

namespace WisDom.SmartTMS.Services
{
    /// <summary>
    /// 合同
    /// </summary>
    [RemoteService(IsEnabled = true)]//设置flse则不自动生成API
    public class TMSContractCarrierService : SmartTMSAppService, ITMSContractCarrierService
    {
        private readonly IRepository<Contract_Carriage, Guid> _repository;
        private readonly IRepository<BusinessAudit, Guid> _adt;//审核记录
        private readonly IRepository<BusinessAuditLogs, Guid> _adtLog;//审核记录
        public TMSContractCarrierService(IRepository<Contract_Carriage, Guid> repository, IRepository<BusinessAudit, Guid> adt, IRepository<BusinessAuditLogs, Guid> adtLog)
        {
            _repository = repository;
            _adt = adt;
            _adtLog = adtLog;
        }
        /// <summary>
        /// 添加合同
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TMSContractCarrierDto> AddCarrierAsync([FromBody]TMSContractCarrierDto data)
        {
            try
            {
                //拼接 创建合同单号
                var worker = new IdWorker(1, 1);
                long num = worker.NextId();
                String snum = "HT" + num;


                Contract_Carriage contract = new Contract_Carriage(
                        GuidGenerator.Create(),
                        DateTime.Now,
                        data.CreatorId,
                        data.Deleted,
                        snum,
                        data.ContractTitle,
                        data.CarriageUnit,
                        data.CarriageMan,
                        data.Line,
                        data.FreightRates,
                        data.CharterConditionTonnage,
                        data.CharterValue,
                        DateTime.Now,
                        data.Operator,
                        data.ContractMoney,
                        data.ProjectDescription,


                        data.MainClause,
                        data.Accessory,
                        //添加默认状态为0  0为待审核
                        data.State = 0,
                        data.Approver
                        );
                var result = await _repository.InsertAsync(contract);
                //添加审核信息 记录
                BusinessAudit audit = new BusinessAudit(
                    GuidGenerator.Create(),
                    result.Id,
                    Enums.BusinessType.Carriage,
                    DateTime.Now,
                    GuidGenerator.Create(),
                    data.Approver
                );
                //审核记录日志
                string a = "39ff0018-7f5a-6039-2e7a-1bf4c4787611";
                Guid c = new Guid(a);
                audit.AddAuditLogs(audit.Id, c, data.Approver, data.State);
                var zzz = _adtLog.InsertAsync(audit.BusinessAuditLogs[0]);

                await _adt.InsertAsync(audit); //持久化

                return ObjectMapper.Map<Contract_Carriage, TMSContractCarrierDto>(result);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 删除合同
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DeleteCarrierAsync(Guid ids)
        {
            try
            {
                await _repository.DeleteAsync(ids);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        /// <summary>
        /// 显示合同
        /// </summary>
        /// <returns></returns>
        public async Task<List<TMSContractCarrierDto>> GetCarrierAsync(string ContractTitle)
        {
            var lst = await _repository.GetListAsync();

            return ObjectMapper.Map<List<Contract_Carriage>, List<TMSContractCarrierDto>>(lst);
        }
        /// <summary>
        /// 获取一个合同
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TMSContractCarrierDto> GetTheCarrierAsync(Guid id)
        {
            var res = await _repository.GetAsync(id);
            return ObjectMapper.Map<Contract_Carriage, TMSContractCarrierDto>(res);
        }
        /// <summary>
        /// 修改合同
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<TMSContractCarrierDto> UpdateCarrierAsync([FromBody]TMSContractCarrierDto data)
        {
            try
            {
                Contract_Carriage res = ObjectMapper.Map<TMSContractCarrierDto, Contract_Carriage>(data);
                var result = await _repository.UpdateAsync(res);
                return ObjectMapper.Map<Contract_Carriage, TMSContractCarrierDto>(result);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}

